package com.bootcamp.servlet.util;

import com.bootcamp.servlet.mvc.model.Custom;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * @program: wrok-1101
 * @description: auth whether a user has login or not
 * @author: Keyu Li
 * @create: 2021-11-05 21:37
 **/

public class AuthLoginUtils {

    /**
     *
     * @param request:
     * @param writer:
     * @return void
     * @date 11/6/2021 10:07
     */
    public static void authLoginStatus(HttpServletRequest request, PrintWriter writer) {
        // 获取userid
        HttpSession session = request.getSession();
        Custom custom = (Custom) session.getAttribute("custom");
        if (custom == null) {
            String login = request.getContextPath() + "/LoginServlet";
            writer.println("<div><a href='registerInterface.html'>注册</a>&nbsp;&nbsp;<a href='loginInterface.html'>登录</a></div>");
            return;
        }
        String logoff = request.getContextPath() + "/LoginServlet?logoff=1";
        writer.println("<div>欢迎您，高贵的 " + custom.getUsername() +"(id:"+custom.getId()+")"
                + "&nbsp;&nbsp;<a href='"+logoff+"'>退出</a></div>");
    }
}
